package com.syh.jn.scm.service.impl.downstream.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.exception.IllegalParameterException;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.system.DownAccount;
import com.syh.jn.scm.domain.downstream.event.system.DownAccountEvent;
import com.syh.jn.scm.domain.downstream.repository.system.DownAccountRepository;
import com.syh.jn.scm.dto.downstream.system.downAccount.DownAccountMto;
import com.syh.jn.scm.dto.downstream.system.downAccount.DownAccountSto;
import com.syh.jn.scm.dto.downstream.system.downAccount.DownAccountVo;
import com.syh.jn.scm.service.api.downstream.system.DownAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DownAccountServiceImpl extends AbstractServiceImpl<DownAccountSto, DownAccountMto, DownAccountVo, DownAccount, Long> implements DownAccountService {

  @Autowired
  private DownAccountRepository downAccountRepository;


  @Override
  protected AggregateRepository<DownAccount, Long> getAggregateRepository() {
    return this.downAccountRepository;
  }

  @Override
  public Long save(DownAccountSto accountSto) {
    DownAccount downAccount = new DownAccount(accountSto);
    return downAccountRepository.add(downAccount);
  }

  @Override
  public void update(DownAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ExtAssert.notNull(mto.getId(), "id为空");
    DownAccount downAccount = downAccountRepository.getById(mto.getId());
    if (downAccount != null) {
      downAccount.modify(mto);
      downAccountRepository.update(downAccount);
    }
  }

  @Override
  public void delete(Long id) {
    ExtAssert.notNull(id, "id为空");
    DownAccount downAccount = downAccountRepository.getById(id);
    DownAccount eventAccount = new DownAccount();
    ExtBeanUtils.copyProperties(downAccount, eventAccount);
    if (downAccount != null) {
      downAccount.destory();
      downAccountRepository.removeById(id);
      this.publicEvent(DownAccountEvent.buildDownAccountDeleted(new Object[]{eventAccount}));
    }
  }


  @Override
  public void changeEable(Long accountId, Short enable) {
    ExtAssert.notNull(accountId, "id为空");
    DownAccount downAccount = downAccountRepository.getById(accountId);
    DownAccount eventAccount = new DownAccount();
    ExtBeanUtils.copyProperties(downAccount, eventAccount);
    if (downAccount != null) {
      downAccount.changeEnable(enable);
      downAccountRepository.update(downAccount);
      if (enable.equals(Short.valueOf("-1")) && eventAccount.getLoginPhoneId() != null)
        this.publicEvent(DownAccountEvent.buildDownAccountDisabled(new Object[]{eventAccount}));
    }
  }

  @Override
  public void setRole(Long accountId, Long roldId) {
    ExtAssert.notNull(roldId, "id为空");
    DownAccount downAccount = downAccountRepository.getById(accountId);
    if (downAccount != null) {
      downAccount.changeRole(roldId);
      downAccountRepository.update(downAccount);
    }
  }

  @Override
  public DownAccountVo verifyAccountPassword(String loginAccount, String password) {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(password, "密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    DownAccount downAccount = downAccountRepository.getOne(wrapper);
    if (downAccount == null) {
      return null;
    }
    if (PasswordUtils.verify(password, downAccount.getLoginPassword())) {
      return downAccountRepository.getOneView(wrapper);
    } else {
      return null;
    }
  }

  @Override
  public void updataPassword(String loginAccount, String oldPassword, String newPassword) throws IllegalParameterException {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(oldPassword, "旧密码为空");
    ExtAssert.notNull(newPassword, "新密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    DownAccount downAccount = downAccountRepository.getOne(wrapper);
    ExtAssert.notNull(downAccount, "找不到该账户");
    if (PasswordUtils.verify(oldPassword, downAccount.getLoginPassword())) {
      downAccount.setLoginPassword(newPassword);
      downAccountRepository.update(downAccount);
    } else {
      throw new IllegalParameterException("旧密码不正确");
    }
  }

  @Override
  public void bindingLoginPhone(Long id, Long phoneId) {
    DownAccount downAccount = downAccountRepository.getById(id);
    if (downAccount != null && phoneId != null) {
      downAccount.bindingLoginPhone(phoneId);
      downAccountRepository.update(downAccount);
    }
  }

  @Override
  public void untyingLoginPhone(Long id) {
    DownAccount downAccount = downAccountRepository.getById(id);
    DownAccount eventAccount = new DownAccount();
    ExtBeanUtils.copyProperties(downAccount, eventAccount);
    if (downAccount != null) {
      downAccount.untyingLoginPhone();
      downAccountRepository.update(downAccount);
      this.publicEvent(DownAccountEvent.buildDownAccountPhoneUntyed(new Object[]{eventAccount}));
    }
  }

  @Override
  public boolean identityAdmin(Long id) {
    Short admin = downAccountRepository.getAdminById(id);
    if (admin != null && admin.equals(Short.valueOf("1"))) {
      return true;
    }
    return false;
  }
}
